package com.Servlet;
import com.DAO.StudentDao;
import com.Student;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/studentManagement")
public class StudentServlet extends HttpServlet {
    private StudentDao studentDao;

    @Override
    public void init() throws ServletException {
        studentDao = new StudentDao();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter("action");

        if ("delete".equals(action)) {
            // 删除学生
            int id = Integer.parseInt(request.getParameter("id"));
            try {
                studentDao.deleteStudent(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
                //response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "删除学生失败");
            }
            System.out.println("删除学生成功"+id);
            request.setAttribute("message", "删除学生成功");
            response.sendRedirect("studentManagement");
        } else if ("edit".equals(action)) {
            // 编辑学生
            int id = Integer.parseInt(request.getParameter("id"));
            Student student = null;
            try {
                student = studentDao.getStudentById(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            request.setAttribute("student", student);
            request.getRequestDispatcher("editStudent.jsp").forward(request, response);
        } else {
            String search = request.getParameter("search");
            // 查询学生列表
            int page = Integer.parseInt(request.getParameter("page") == null ? "1" : request.getParameter("page"));
            int pageSize = 5; // 每页显示5个学生
            List<Student> students = null;
            try {
                if (search != null && !search.isEmpty()){
                    // 如果有搜索条件，则执行搜索
                    students = studentDao.searchStudents(search, page, pageSize);
                } else {
                    // 如果没有搜索条件，则显示所有学生
                    students = studentDao.getStudents(page, pageSize);
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            // 获取总学生数量
            int totalStudents = 0;
            try {
                totalStudents = studentDao.getTotalStudents();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            int totalPages = (int) Math.ceil((double) totalStudents / pageSize);

            // 调试信息
            System.out.println("学生总数: " + totalStudents);
            //System.out.println("学生数: " + (students == null ? 0 : students.size()));
            System.out.println("Fetching students for page: " + page + " with page size: " + pageSize);

            // 将学生列表和分页信息传递给 JSP 页面
            request.setAttribute("students", students);
            request.setAttribute("totalPages", totalPages);
            request.setAttribute("currentPage", page);
            request.getRequestDispatcher("studentManagement.jsp").forward(request, response);
        }
    }
}


